package com.pocketsweet.ui;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.uilib.ClearEditText;
import com.pocketsweet.ui.uilib.adapter.BaseListAdapter;
import com.pocketsweet.utils.StringUtils;
import com.pocketsweet.utils.ToolKits;

@ContentView(R.layout.activity_addfriend)
public class AddFriend extends BaseActivity {

	@ViewInject(R.id.filter_edit)
	private ClearEditText mClearEditText;

	@ViewInject(R.id.friendlist)
	private ListView friendlistView;
	@ViewInject(R.id.tvSearch)
	private TextView tvSearch;

	private FindFriendAdapater adapter;
	List<MLUser> friendlist = new ArrayList<MLUser>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		initView();
	}

	private void initView() {
		// 根据输入框输入值的改变来过滤搜索
		mClearEditText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// 当输入框里面的值为空，更新为原来的列表，否则为过滤数据列表

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {

			}

			@Override
			public void afterTextChanged(Editable s) {
			}
		});

		adapter = new FindFriendAdapater(this, friendlist);
		friendlistView.setAdapter(adapter);
		friendlistView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				MLUser user = (MLUser) adapter.getItem(position);

				Intent intent = new Intent(AddFriend.this, Profile.class);
				intent.putExtra("userId", user.getObjectId());
				AddFriend.this.startActivity(intent);

			}
		});
	}

	@OnClick(R.id.tvSearch)
	public void onSearchClick(View v) {
		String id = mClearEditText.getText().toString();
		if(StringUtils.isEmpty(id)){
			return;
		}
		AVQuery<MLUser> query = new AVQuery<MLUser>("_User");
		query.whereEqualTo("molianId", Integer.valueOf(id));
		query.findInBackground(new FindCallback<MLUser>() {

			@Override
			public void done(List<MLUser> users, AVException e) {
				if (e == null) {
					if (users.size() == 0) {
						ToolKits.toast(AddFriend.this, "没有此人");
					} else {
						adapter.setDatas(users);
						adapter.notifyDataSetChanged();
					}
				} else {
					ToolKits.toast(AddFriend.this, "查找失败，请检查网络");
				}

			}
		});
	}

	/**
	 * adapter
	 * 
	 */
	private class FindFriendAdapater extends BaseListAdapter<MLUser> {
		private Context context;

		public FindFriendAdapater(Context ctx) {
			super(ctx);
			this.context = ctx;
		}

		public FindFriendAdapater(Context ctx, List<MLUser> datas) {
			super(ctx, datas);
			this.context = ctx;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.item_findfriend, null);
			}
			// 设置姓名
			MLUser user = datas.get(position);
			TextView name = (TextView) convertView
					.findViewById(R.id.tvuserName);
			name.setText(user.getNickname());
			// id
			TextView pocketId = (TextView) convertView
					.findViewById(R.id.tvPocketId);
			pocketId.setText("(" + user.getMolianId() + ")");
			// 设置头像
			ImageView avatar = (ImageView) convertView
					.findViewById(R.id.imgAvatar);
			UserService.displaySquare(user.getAvatarUrl(), avatar);

			return convertView;
		}

	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}
}
